package org.com.app.knock.fragment;

import java.util.HashMap;

import org.com.app.knock.activity.ProfileActivity;
import org.com.app.knock.activity.SplashActivity;
import org.com.app.knock.activity.UnblockActivity;
import org.com.app.knock.view.LoadingView;
import org.json.JSONObject;

import z.lib.base.BaseServiceCallBack;
import z.lib.base.BaseUtils;
import z.lib.base.BaseUtils.API;
import z.lib.base.CommonAndroid;
import z.lib.base.LogUtils;
import z.lib.base.callback.RestClient.RequestMethod;
import android.content.Intent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.TextView;

import com.acv.libs.nbase.BaseFragment;
import com.app.knock.db.Account;
import com.knock.app.R;

public class MenuFragment extends BaseFragment implements AnimationListener {
	private static final String TAG = "MenuFragment";
	View views;
	private LoadingView loading;
	public MenuFragment() {
		super();
	}

	@Override
	public void setUpFragment(View view) {
		this.views = view;
		loading = CommonAndroid.getView(view, R.id.loading);
		CommonAndroid.getView(view, R.id.btn_menu_back).setOnClickListener(this);
		CommonAndroid.getView(view, R.id.menu_1).setOnClickListener(this);
		CommonAndroid.getView(view, R.id.menu_2).setOnClickListener(this);
		CommonAndroid.getView(view, R.id.menu_3).setOnClickListener(this);
		CommonAndroid.getView(view, R.id.menu_4).setOnClickListener(this);
//		CommonAndroid.getView(view, R.id.menu_5).setOnClickListener(this);
//		CommonAndroid.getView(view, R.id.menu_6).setOnClickListener(this);
		CommonAndroid.getView(view, R.id.menu_7).setOnClickListener(this);
		CommonAndroid.getView(view, R.id.menu_8).setOnClickListener(this);
		CommonAndroid.getView(view, R.id.menu_9).setOnClickListener(this);
		TextView login_name = CommonAndroid.getView(views, R.id.menu_22);
		login_name.setText(new Account(getActivity()).getUserName());
	}

	@Override
	public int layoytResurce() {
		return R.layout.menu;
	}
	
	@Override
	public void onClick(View v) {
		super.onClick(v);
		int idView = v.getId();
		Intent Page;
		switch (idView) {
		case R.id.btn_menu_back:
			getActivity().finish();
			break;
		case R.id.menu_9:
			logOut();
			
			break;
		case R.id.menu_4:
			Page = new Intent(getActivity(), UnblockActivity.class);
			getActivity().startActivityForResult(Page,0);
			break;
		/*case R.id.menu_5:
			//receved
			Page = new Intent(getActivity(), MessageRecevedActivity.class);
			getActivity().startActivityForResult(Page,0);
			break;
		case R.id.menu_6:
			//send
			Page = new Intent(getActivity(), MessageSendActivity.class);
			getActivity().startActivityForResult(Page,0);
			break;*/
//		case R.id.menu_2:
//			Page = new Intent(getActivity(), ProfileActivity.class);
//			getActivity().startActivityForResult(Page,0);
//			break;
		default:
			break;
		}
	}

	protected void logOut() {
		// TODO Auto-generated method stub
		HashMap<String, String> params = new HashMap<String, String>();
		BaseUtils.execute(getActivity(), RequestMethod.POST, API.API_USER_LOGOUT, params, callbacklogout);
	}
	
	private BaseServiceCallBack callbacklogout = new BaseServiceCallBack() {

		public void onStart() {
			CommonAndroid.showView(true, loading);
			CommonAndroid.hiddenKeyBoard(getActivity());
		};

		public void onError(String message) {
			CommonAndroid.showView(false, loading);
			if (!isFinish()) {
				LogUtils.e(TAG,"err=="+message);
			}
		};

		public void onSucces(String response) {
			CommonAndroid.showView(false, loading);
			if (!isFinish()) {
				JSONObject mainJsonObject;
				try {
					mainJsonObject = new JSONObject(response);
					String is_succes = CommonAndroid.getString(mainJsonObject,
							BaseUtils.KEY.is_success);
					LogUtils.e(TAG,"logout"+ mainJsonObject.toString());
					Intent Page = new Intent(getActivity(), SplashActivity.class);
					getActivity().startActivityForResult(Page,0);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		};
	};


	@Override
	public void onAnimationStart(Animation animation) {

	}

	@Override
	public void onAnimationEnd(Animation animation) {
		mHandle.sendEmptyMessage(0);
	}

	@Override
	public void onAnimationRepeat(Animation animation) {

	}
}